﻿using System;
using System.IO;
using Mono.Cecil.PE;

namespace Mono.Cecil.Cil
{
	// Token: 0x02000151 RID: 337
	public class DefaultSymbolReaderProvider : ISymbolReaderProvider
	{
		// Token: 0x06000C41 RID: 3137 RVA: 0x0000663F File Offset: 0x0000483F
		public DefaultSymbolReaderProvider() : this(true)
		{
		}

		// Token: 0x06000C42 RID: 3138 RVA: 0x0000664A File Offset: 0x0000484A
		public DefaultSymbolReaderProvider(bool throwIfNoSymbol)
		{
			this.throw_if_no_symbol = throwIfNoSymbol;
		}

		// Token: 0x06000C43 RID: 3139 RVA: 0x00035AA0 File Offset: 0x00033CA0
		public ISymbolReader GetSymbolReader(ModuleDefinition module, string fileName)
		{
			bool flag = module.Image.HasDebugTables();
			ISymbolReader result;
			if (flag)
			{
				result = null;
			}
			else
			{
				bool hasDebugHeader = module.HasDebugHeader;
				if (hasDebugHeader)
				{
					ImageDebugHeader debugHeader = module.GetDebugHeader();
					ImageDebugHeaderEntry embeddedPortablePdbEntry = debugHeader.GetEmbeddedPortablePdbEntry();
					bool flag2 = embeddedPortablePdbEntry != null;
					if (flag2)
					{
						return new EmbeddedPortablePdbReaderProvider().GetSymbolReader(module, fileName);
					}
				}
				string pdbFileName = Mixin.GetPdbFileName(fileName);
				bool flag3 = File.Exists(pdbFileName);
				if (flag3)
				{
					bool flag4 = Mixin.IsPortablePdb(Mixin.GetPdbFileName(fileName));
					if (flag4)
					{
						return new PortablePdbReaderProvider().GetSymbolReader(module, fileName);
					}
					try
					{
						return SymbolProvider.GetReaderProvider(SymbolKind.NativePdb).GetSymbolReader(module, fileName);
					}
					catch (Exception)
					{
					}
				}
				string mdbFileName = Mixin.GetMdbFileName(fileName);
				bool flag5 = File.Exists(mdbFileName);
				if (flag5)
				{
					try
					{
						return SymbolProvider.GetReaderProvider(SymbolKind.Mdb).GetSymbolReader(module, fileName);
					}
					catch (Exception)
					{
					}
				}
				bool flag6 = this.throw_if_no_symbol;
				if (flag6)
				{
					throw new SymbolsNotFoundException(string.Format("No symbol found for file: {0}", fileName));
				}
				result = null;
			}
			return result;
		}

		// Token: 0x06000C44 RID: 3140 RVA: 0x00035BB8 File Offset: 0x00033DB8
		public ISymbolReader GetSymbolReader(ModuleDefinition module, Stream symbolStream)
		{
			bool flag = module.Image.HasDebugTables();
			ISymbolReader result;
			if (flag)
			{
				result = null;
			}
			else
			{
				bool hasDebugHeader = module.HasDebugHeader;
				if (hasDebugHeader)
				{
					ImageDebugHeader debugHeader = module.GetDebugHeader();
					ImageDebugHeaderEntry embeddedPortablePdbEntry = debugHeader.GetEmbeddedPortablePdbEntry();
					bool flag2 = embeddedPortablePdbEntry != null;
					if (flag2)
					{
						return new EmbeddedPortablePdbReaderProvider().GetSymbolReader(module, "");
					}
				}
				Mixin.CheckStream(symbolStream);
				Mixin.CheckReadSeek(symbolStream);
				long position = symbolStream.Position;
				BinaryStreamReader binaryStreamReader = new BinaryStreamReader(symbolStream);
				int num = binaryStreamReader.ReadInt32();
				symbolStream.Position = position;
				bool flag3 = num == 1112167234;
				if (flag3)
				{
					result = new PortablePdbReaderProvider().GetSymbolReader(module, symbolStream);
				}
				else
				{
					byte[] array = binaryStreamReader.ReadBytes("Microsoft C/C++ MSF 7.00".Length);
					symbolStream.Position = position;
					bool flag4 = true;
					for (int i = 0; i < array.Length; i++)
					{
						bool flag5 = array[i] != (byte)"Microsoft C/C++ MSF 7.00"[i];
						if (flag5)
						{
							flag4 = false;
							break;
						}
					}
					bool flag6 = flag4;
					if (flag6)
					{
						try
						{
							return SymbolProvider.GetReaderProvider(SymbolKind.NativePdb).GetSymbolReader(module, symbolStream);
						}
						catch (Exception)
						{
						}
					}
					long num2 = binaryStreamReader.ReadInt64();
					symbolStream.Position = position;
					bool flag7 = num2 == 5037318119232611860L;
					if (flag7)
					{
						try
						{
							return SymbolProvider.GetReaderProvider(SymbolKind.Mdb).GetSymbolReader(module, symbolStream);
						}
						catch (Exception)
						{
						}
					}
					bool flag8 = this.throw_if_no_symbol;
					if (flag8)
					{
						throw new SymbolsNotFoundException(string.Format("No symbols found in stream", Array.Empty<object>()));
					}
					result = null;
				}
			}
			return result;
		}

		// Token: 0x0400078B RID: 1931
		private readonly bool throw_if_no_symbol;
	}
}
